package com.sdemo.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.sdemo.common.globals.Globals;
import com.sdemo.modules.comm.controller.BaseController;
import com.sdemo.modules.sys.model.Menu;
import com.sdemo.modules.sys.model.MenuTree;
import com.sdemo.modules.sys.model.Role;
import com.sdemo.modules.sys.model.User;
import com.sdemo.modules.sys.service.impl.MenuServiceImpl;
import com.sdemo.modules.sys.service.impl.RoleServiceImpl;
import com.sdemo.modules.sys.service.impl.UserServiceImpl;
import com.sdemo.utils.Convert;
import com.sdemo.utils.Pager;
import com.sdemo.utils.Ret;

@Controller
@RequestMapping("/sys")
public class RoleController extends BaseController {
	@Autowired
	private RoleServiceImpl roleServiceImpl; 
	@Autowired
	private MenuServiceImpl menuServiceImpl; 
	
	/**
    * 分页显示角色信息
    */
   @RequestMapping(value={"/rolelist2"})
   @ResponseBody
   public  Pager<Role> rolelist(HttpServletRequest request){
	   
	   Map<String, Object> parms = super.getParamValues(request);
	   
	   parms =super.pageXX(parms, request);
	   
	   List<Role> roleList = roleServiceImpl.queryPageRole(parms);
	   
	   int total = roleServiceImpl.queryCount(parms);
	   Pager<Role> page = new Pager<Role>();
	   page.setRows(roleList);
	   page.setTotal(total);
	   
       return page;
   }
   /**
    * 通过ROLE_ID角色信息
    */
   @RequestMapping(value={"/queryRoleById"})
	@ResponseBody
	public Ret queryUserById(Long roleid){
		Role role = roleServiceImpl.queryRoleById(roleid);
		if(role == null){
			Ret.error("数据为空");
		}
		return Ret.ok().put("data", role);
	}
   /**
    * 删除角色信息
    */
   @RequestMapping(value={"/deleteRole"})
   @ResponseBody
   public Ret deleteRole(Role role,Long roleid){
	   int index = roleServiceImpl.deleteRole(roleid);
	   Ret ret = new Ret();
	   if(index>0){
		   ret.put("code", 0); 
	   }
	   return ret;
   }
   
   /**
    * 新增角色信息
    */
   @RequestMapping(value={"/insertRole"})
   @ResponseBody
   public Ret insertRole(Role role,Long mid){
	   
	   
	   if(role.getRemark().equals("0")){
		   role.setRemark("管理组");
	   }else if(role.getRemark().equals("1")){
		   role.setRemark("产品组");
	   }else{
		   role.setRemark("用户组");
	   }
	   roleServiceImpl.insertRole(role);
	   String[] str = role.getMids().split(",");
	   List<Menu> list = new ArrayList<>();
	   for(int i = 0 ;i < str.length; i++){
		   Menu addrolemenu = new Menu();
		   addrolemenu.setMid(Convert.getLong(str[i]));
		   addrolemenu.setRoleId(role.getRoleid());
		   list.add(addrolemenu);
	   }
	   int addindex = roleServiceImpl.insertRoleMenuBatch(list);
	   if(addindex>0){
		   return Ret.ok();
	   }
	   return Ret.error("失败");
   }
   
   
   /**
    * 根据用户id 查询 角色name
    * 
    */
   @RequestMapping(value={"/queryRoleByUserId"})
 	@ResponseBody
 	public Ret queryRoleByUserId(){
	   List<Role> rolelist = null;
	   	if(Globals.SUPSER_ADMIN.longValue()==getUserId().longValue()){
	   		rolelist = roleServiceImpl.queryAllRole();
	   	}else{
	   		rolelist = roleServiceImpl.queryRoleByUserId(getUserId());
	   	}
	   	
	   	for(int i=0;i<rolelist.size();i++){
	   		Role role = rolelist.get(i);
	   		if(role.getUserId().longValue()==getUserId().longValue()){
	   			role.setCheckeds(2);//需要打钩的
	   		}
	   	}
 		if(null == rolelist || rolelist.size()<1){
 			Ret.error("数据为空");
 		}
 		return Ret.ok().put("data", rolelist);
 	}
   /**
    * 根据用户id和roleid 查询 角色name
    * 
    */
   @RequestMapping(value={"/queryRoleAll"})
 	@ResponseBody
 	public Ret queryRoleAll(){
	   List<Role> rolelist = null;
	   	for(int i=0;i<rolelist.size();i++){
	   		Role role = rolelist.get(i);
	   		if(role.getUserId().longValue()==getUserId().longValue()){
	   			//role.setCheckeds(2);//需要打钩的
	   			rolelist.get(i).setCheckeds(2);
	   		}
	   	}
 		if(null == rolelist || rolelist.size()<1){
 			Ret.error("数据为空");
 		}
 		return Ret.ok().put("data", rolelist);
 	}
   /**
    * 加载修改 角色信息
    * 
    */
    @RequestMapping(value={"/loadRoleMenu"})
 	@ResponseBody
 	//@RequiresPermissions(value={"sys:man:user","sys:man:sys"},logical=Logical.OR)//权限控制
 	public Ret loadRoleMenu(Role role){
	   List<MenuTree> list = menuServiceImpl.queryMenuByRoleid(role.getRoleid());
	   for(int i = 0; i < list.size();i++){
   		if(list.get(i).getMcount()==2 || list.get(i).getId() == 1){
   			list.get(i).setChecked(true);
   		}
   	}
	   Role roleload = roleServiceImpl.queryRoleById(role.getRoleid());
	   
	   return Ret.ok().put("data", list).put("role", roleload);
   }
 	
 	
 	 /**
     * 修改 角色信息
     * 
     */
    @RequestMapping(value={"/updateRoleMenu"})
  	@ResponseBody
  	public Ret updateRole(Role role){
    	
    	String[] arrStr = role.getMids().split(",");
    	List<Menu> updatelist = new ArrayList<>();
    	for(int i = 0 ; i<arrStr.length;i++){
    		Menu menu = new Menu();
    		menu.setMid(Convert.getLong(arrStr[i]));
    		menu.setRoleId(role.getRoleid());
    		updatelist.add(menu);
    	}
    	
    	roleServiceImpl.deleteRoleMenuByRoleId(role.getRoleid());
    	roleServiceImpl.insertRoleMenuBatch(updatelist);
		myJedis.hdel(Globals.redis_userauth_key);
  		return Ret.ok();
  	}
}
